/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aaa.schedule.parameters;

import aaa.main.R;
import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

/**
 *
 * @author Seb
 */
public class ScheduleProjectId extends ParameterType {
	
	public static final String FIELD_ID = "project_id" ;

	private int projectId ;
	private LayoutInflater inflater;

	private Button esialButton;
	private Button uhpButton;

	public ScheduleProjectId  (LayoutInflater inflater, String projectId) {
		super(FIELD_ID) ;
		this.inflater = inflater ;
		try {
			this.projectId = Integer.parseInt(projectId);
		} catch (NumberFormatException e) {
			this.projectId = 0 ;
		}
	}
	
	public void setProjectId(int projectId) {
		this.projectId = projectId;
	}

	@Override
	public Object getValue(View view) {
		return this.projectId ;
	}

	@Override
	public View getView(View convertView, ViewGroup parent) {
		convertView = this.inflater.inflate(R.layout.type_schedule_project_id, null);

		this.init(convertView) ;
		this.listener() ;

		return convertView;
	} 

	public void init(View convertView) {
		this.uhpButton   = (Button) (TextView) convertView.findViewById(R.id.uhp) ;
		this.esialButton = (Button) (TextView) convertView.findViewById(R.id.esial) ;

		//positionne les boutons selon projectId
		if (this.projectId != 0) {
			Button selected = null ;
			Button other = null ;
			if (this.projectId == 1) {
				selected = this.uhpButton ;
				other = this.esialButton ;
			} else {/* this.projectId == 2 */
				selected = this.esialButton ;
				other = this.uhpButton ;
			}// if
			selected.setBackgroundColor(Activity.RESULT_OK);
			selected.setClickable(false);  
			other.setClickable(true); 
			other.setBackgroundColor(Color.GRAY);
		} else {
			this.uhpButton.setBackgroundColor(Activity.RESULT_OK);
			this.esialButton.setBackgroundColor(Activity.RESULT_OK);
		}// if
	}

	public void listener() {         
		this.uhpButton.setOnClickListener(new ProjectIdListener(this.uhpButton, this.esialButton, 1, this));
		this.esialButton.setOnClickListener(new ProjectIdListener(this.esialButton, this.uhpButton, 2, this));
	}

	class ProjectIdListener implements OnClickListener {

		protected Button button ;
		protected Button dualButton ;
		protected int projectId ;
		protected ScheduleProjectId sp ;	

		/**
		 * @param button
		 * @param dualButton
		 * @param projectId
		 * @param sp
		 */
		public ProjectIdListener(Button button, Button dualButton,
				int projectId, ScheduleProjectId sp) {
			super();
			this.button = button;
			this.dualButton = dualButton;
			this.projectId = projectId;
			this.sp = sp;
		}

		public void onClick(View v) {
			this.button.setBackgroundColor(Activity.RESULT_OK);
			this.button.setClickable(false);
			this.button.setSelected(false) ;
			this.sp.setProjectId(this.projectId) ;

			this.dualButton.setClickable(true); 
			this.dualButton.setBackgroundColor(Color.GRAY); 
			this.dualButton.setSelected(false) ;
		}
	}

}
